﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_PageEdit : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            string selectedValue="";
            DataTable newTable = null;
            this.PageLink.NavigateUrl = "Page.aspx?menuId=" + str;
            this.MenuName.Text = str2;
            IDb db = new DbFactory().DriverDb();
            db.Open();
            if (!base.IsPostBack)
            {
                DataTable oldTable = db.DataTable("Title,ParentId,SinglePageId", "SinglePage", string.Format("MenuId={0}", str), "OrderId ASC");
                oldTable.Columns.Add("TitleText");
                newTable = oldTable.Clone();
                Function.PopulateTable(oldTable, newTable, "Title", "SinglePageId", "ParentId", 0, 0);
                selectedValue = base.Request.QueryString["singlePageId"];
                if (newTable != null)
                {
                    this.SinglePageIdList.DataSource = newTable;
                    this.SinglePageIdList.DataTextField = "TitleText";
                    this.SinglePageIdList.DataValueField = "SinglePageId";
                    this.SinglePageIdList.DataBind();
                }
                if ((selectedValue == null) | (selectedValue == ""))
                {
                    selectedValue = this.SinglePageIdList.SelectedValue;
                }
            }
            else
            {
                selectedValue = this.SinglePageIdList.SelectedValue;
            }
            DataRow row = db.TopRow("PageContent", "singlePage", string.Format("SinglePageId={0}", selectedValue), "");
            db.Close();

            if (row != null)
            {
                this.PageContent.Text = (string)row["PageContent"];
            }
            this.SinglePageIdList.SelectedValue = selectedValue;
            this.UpDate.Enabled = true;
            if (!ManagerLogin.ValidateRole(string.Format("0_{0}_c", this.SinglePageIdList.SelectedValue)))
            {
                this.Msg.CssClass = "errorMsg";
                this.Msg.Text = "您无此权限";
                this.Msg.Visible = true;
                this.UpDate.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        this.Msg.CssClass = "msg";
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }

    protected void SinglePageIdList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Bind();
    }

    protected void UpDate_Click(object sender, EventArgs e)
    {
        string[,] includeColumns = new string[,] { 
            { "PageContent", this.PageContent.Text.ToString(), "NText", "0" } 
        };
        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Update("singlePage", includeColumns, string.Format("SinglePageId ={0}", this.SinglePageIdList.SelectedValue));
        db.Close();
        this.Msg.Text = "内容更新成功！";
        this.Msg.Visible = true;
    }
}
